from peewee import BigAutoField, BigIntegerField, DateField, TimeField, BooleanField, IntegerField
from common.model.abstrct import AbstractModel, IntEnumField, AbstractModelWithID
from enums.stock_enum import StockTypeEnum


class MallStock(AbstractModel):

    class Meta:
        table_name = "tb_mall_stock"
        indexes = (
            (("mall_id", "spu_id"), False),
        )

    stock_id = BigAutoField(primary_key=True)
    mall_id = BigIntegerField(help_text="mall id")
    spu_id = BigIntegerField(help_text="spu id")
    sku_id = BigIntegerField(help_text="sku id")
    # stock_type = 1 总库存 则stock_item 的 start_date, end_date, start_time, end_time为None
    # stock_type = 2 日历库存 则stock_item 的 start_time, end_time为None
    # stock_type = 3 场次库存 则start_date, end_date, start_time, end_time都不能为None
    stock_type = IntEnumField(choices=StockTypeEnum, help_text="库存类型")


class MallStockItem(AbstractModelWithID):

    class Meta:
        table_name = "tb_mall_stock_item"
        indexes = (
            (("stock_id", ), False),
            (("date", ), False)
        )

    stock_id = BigIntegerField(help_text="库存id")
    quantity = IntegerField(default=0, help_text="库存数量")
    prehold_quantity = IntegerField(default=0, help_text="预扣库存")
    # 表示在start_date->end_date的日期内 每个start_time->end_time的库存是quantity
    date = DateField(help_text="日期", null=True, default="1970-01-01", formats=["%Y-%m-%d"])
    start_time = TimeField(help_text="开始时间", null=True)
    end_time = TimeField(help_text="结束时间", null=True)
    # 比如想在某一天的某一场设置特殊库存,这个字段就会True
    is_special = BooleanField(default=False, help_text="是否是特殊库存")